<!--
 * @Author: typ
 * @Date: 2025-01-15 10:15:08
 * @LastEditors: typ
 * @LastEditTime: 2025-02-28 11:48:36
 * @FilePath: /720Manage-vite/src/views/modelToTask/index.vue
 * @Description: 
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="请输入任务名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" @change="handleQuery" />
      </el-form-item>
      <el-form-item label="任务来源" prop="dataFrom">
        <el-input v-model="queryParams.dataFrom" placeholder="请输入任务来源" clearable style="width: 200px"
          @keyup.enter="handleQuery" @change="handleQuery" />
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-select @change="handleQuery" v-model="queryParams.taskType" placeholder="请选择任务类型" clearable
          style="width: 200px">
          <el-option v-for="item in sys_task_type" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="完成进度" prop="progress">
        <el-select @change="handleQuery" v-model="queryParams.progress" placeholder="请选择完成进度" clearable
          style="width: 200px">
          <el-option label="未完成" :value="0" />
          <el-option label="已完成" :value="100" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="Plus" @click="handleAdd">添加模型</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList" :default-sort="{ prop: 'createTime', order: 'descending' }">
      <el-table-column label="序号" align="center" type="index" width="90" />
      <el-table-column label="任务id" align="center" prop="taskId" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务人" align="center" prop="taskName" />
      <el-table-column label="任务来源" align="center" prop="dataFrom" />
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template #default="scope">
          <dict-tag :options="sys_task_type" :value="scope.row.taskType" />
        </template>
      </el-table-column>
      <el-table-column label="完成进度" align="center" prop="progress" sortable />
      <el-table-column label="任务发起时间" align="center" prop="createTime" sortable width="160" />
      <el-table-column label="操作" width="160" align="center" class-name="small-padding fixed-width"
        style="display: flex; align-items: center; justify-content: center">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)">参数</el-button>
          <el-button v-if="+scope.row.progress === 100" link type="primary"
            @click="handleView(scope.row)">模型预览</el-button>
          <el-button v-if="+scope.row.progress != 100" link type="primary"
            @click="handelProgress(scope.row.taskId)">进度查询</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 参数详情 -->
    <el-dialog title="参数详情" v-model="open" width="800px" append-to-body>
      <el-form :model="form" ref="formRef" label-width="140px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务id：">{{ form.taskId }}</el-form-item>
            <el-form-item label="任务来源：">{{ form.dataFrom }}</el-form-item>
            <el-form-item label="任务名称：">{{ form.taskName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间：">{{ form.createTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成进度：">{{ form.progress }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="关键请求参数json：">{{ form.forwardedRequestParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数json：">{{ form.receiveRequestParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="结果json：">{{ form.resultParam }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { getModelTaskList, delModelTask, queryProgress } from '@/api/model'
import { useRouter } from 'vue-router'
const router = useRouter();
const { proxy } = getCurrentInstance()
const { sys_task_type } = proxy.useDict("sys_task_type");
const open = ref(false)
let total = ref(0)
let loading = ref(false)
let tableList = ref([])

const data = reactive({
  form: {},
  queryParams: {
    taskName: '',
    pageNum: 1,
    pageSize: 10,
  },
});

const { queryParams, form } = toRefs(data);

onMounted(() => {
  getList()
})
// 获取模型列表
const getList = () => {
  loading.value = true
  getModelTaskList(queryParams.value).then(res => {
    tableList.value = res.rows
    total.value = res.total
    loading.value = false
  })
}
// 查询
const handleQuery = () => {
  getList()
}

// 重置查询条件
const resetQuery = () => {
  queryParams.value = {
    name: ''
  }
  getList();
}
// 进度查询
const handelProgress = async (id) => {
  await queryProgress(id);
  proxy.$modal.msgSuccess("操作成功");
  queryParams.value.pageNum = queryParams.value.pageNum;
  getList();
}

/** 表单重置 */
function reset() {
  form.value = {};
  proxy.resetForm("formRef");
}
/** 参数详情按钮操作 */
function view(row) {
  reset();
  form.value = row;
  open.value = true;
}
// 编辑
const handleDetail = (row) => {
  reset();
  form.value = row;
  open.value = true;
}
// 添加模型
const handleAdd = () => {
  router.push({ path: '/addModel' });
}
// 模型预览
const handleView = (row) => {
  if (row.taskType === '1') {
    window.open('/modelView?id=' + row.taskId, '_blank');
  } else {
    window.open('/b3dmPreview?id=' + row.taskId, '_blank');
  }
}
// 删除
const handleDelete = (row) => {
  proxy.$modal.confirm('是否确认删除任务名称为"' + row.taskName + '"的数据项？').then(function () {
    return delModelTask(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

// 定时器
let timer = null
// 轮训
const startTimer = () => {
  if (timer) return
  timer = setInterval(async () => {
    // 过滤出进度不为100的数据
    const unfinishedTasks = tableList.value.filter(item => item.progress !== 100);
    if (unfinishedTasks.length === 0) {
      // 如果没有未完成的任务，清除定时器
      clearInterval(timer)
      timer = null
      return
    } else {
      // 重新获取列表
      getModelTaskList(queryParams.value).then(res => {
        tableList.value = res.rows
        total.value = res.total
      })
    }
  }, 5000) // 5秒执行一次
}

// 组件挂载时启动定时器
onMounted(() => {
  startTimer()
})

// 组件卸载时清除定时器
onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
    timer = null
  }
})
</script>

<style lang="scss" scoped>
::v-deep .el-form-item__content {
  white-space: pre-wrap;
  overflow-y: auto;
}
</style>